var COLSifra = 0;
var COLNaziv = 1;
var COLKolicina = 2;
var COLCena = 3;
var COLKolCena = 4;
var COLPopustPerc = 5;
var COLPopust = 6;
var COLPDV = 7;
var COLUkupno = 8;
var SPANColsInLastRow = 4;

function afterSearchPredracun(response){
	document.getElementById("thePredracun").innerHTML = response;
	predracunInitSpans();
	var tbl = document.getElementById("stavkeTable");
	Predracun(tbl,null);
}

function predracunSavePrint(ugovor,ugovor_id){
	var container = document.getElementById("PredracunContainer");
	var datum_placanja=document.getElementById("valuta_placanja").innerHTML;
	var a = "a=save&datumplacanja="+escape(datum_placanja)+"&ugoid="+escape(ugovor_id)+"&ugovor="+escape(ugovor)+"&content="+escape(container.innerHTML);
	LoadRSDoc("predracun.php","afterPredracunSavePrint",a,1,1);
}
function afterPredracunSavePrint(response){
	var tmp = response.split("{!}",2);
	var code = parseInt(tmp[0]);
	var msg = tmp[1];
	if(!code){
		window.print();
	}else{
		alert(msg);
	}
}

//---------------------------------------------------------------------

function predracunInitSpans(){
	var container = document.getElementById("PredracunContainer");
	var spans = container.getElementsByTagName("span");
	//window.status = spans.length;
	for(var i=0; i<spans.length; i++){
		if(spans[i].className=="value"){
			spans[i].style.cursor = "pointer";
			spans[i].title = "izmeni";
			spans[i].onclick = function(){
				if(this.id=="datumCenovnika")
				{
					var s=String(this.innerHTML);
					var dan=s.substring(0,2);
					var mes=s.substring(3,5);
					var god=s.substring(6,10);
					var nval = prompt("Novi datum (dd.mm.yyyy):",dan+"."+mes+"."+god);
					if (nval==null) return;
					if(!nval.length){
						if(!confirm("Da li želite da izbrišete vrednost?")){
							return;
						}
					}
					s=String(nval);
					dan=s.substring(0,2);
					mes=s.substring(3,5);
					god=s.substring(6,10);
					
					this.innerHTML = nval;
					LoadRSDoc("predracun.php?a=cenovnik&dan="+dan+"&mes="+mes+"&god="+god,"updateCenovnik");
				}
				else if (this.id=="placanje_avans") {
					var a=parseFloat(this.innerHTML.replace("%",""));
					var nval = prompt("nova vrednost:",a)
					if (nval==null) return;
					nval=nval.replace("%","");
					if(!nval.length)
						return;
					this.innerHTML=nval+"%";
					CalculateAvans();
				}
				else if (this.id=="placanje_rate") {
					var txt=this.innerHTML;
					var nval = prompt("nova vrednost:",txt);
					if (nval==null) return;
					if(!nval.length)
						return;
					this.innerHTML=nval;
					CalculateRata();
				}
				else if (this.id=="uplacenodosada") {
					var txt=strip_float_format(this.innerHTML);
					var nval = prompt("nova vrednost:",txt);
					if (nval==null) return;
					if(!nval.length)
						return;
					this.innerHTML=number_format(nval,2);
					CalculateZaUplatu();
				}
				else if (this.id=="zauplatu2") {
					var txt=strip_float_format(this.innerHTML);
					var nval = prompt("nova vrednost:",txt);
					if (nval==null) return;
					if(!nval.length)
						return;
					this.innerHTML=number_format(nval,2);
					document.getElementById("zauplatu1").innerHTML=this.innerHTML;
				}
				else {
					var txt=this.innerHTML.replace("&nbsp;"," ");
					var nval = prompt("nova vrednost:",txt);
					if (nval==null) return;
					if(nval.length==0){
						if(!confirm("Da li želite da izbrišete vrednost?")){
							return;
						}
					}
					this.innerHTML = nval;
					if(this.id=="broj_ugovora_gore"){
						document.getElementById("broj_ugovora_dole").innerHTML = nval;
						document.getElementById("broj_ugovora_dole_dole").innerHTML = nval;
					}
				}
			}
		}
	}
	var tbl = document.getElementById("stavkeTable");
	if(tbl){
		var tds = tbl.getElementsByTagName("td");
		for(i=0; i<tds.length; i++){
			if(tds[i].className=="edit"){
				tds[i].onclick = OnCellStavkeTabeleClick;
			}
		}
	}
}
	
function CalculateRata()
{
	var br_rata=strip_float_format(document.getElementById("placanje_rate").innerHTML);
	var a = strip_float_format(document.getElementById("ugovoreni_avans").innerHTML);
	var c=strip_float_format(document.getElementById("Ukupno").innerHTML);
	if (br_rata=="" || br_rata==0)
		document.getElementById("ugovorena_rata").innerHTML="0,00";
	else document.getElementById("ugovorena_rata").innerHTML=number_format((c-a)/br_rata,2);
	CalculateZaUplatu();
}

function strip_float_format(f)
{
	return parseFloat(strip_number_format(f));
}

function CalculateAvans()
{
	var a = parseFloat(document.getElementById("placanje_avans").innerHTML.replace("%",""));
	var c=strip_float_format(document.getElementById("Ukupno").innerHTML);
	document.getElementById("ugovoreni_avans").innerHTML=number_format(a/100*c,2);
	CalculateRata();
}

function CalculateZaUplatu()
{
	var uplaceno = strip_float_format(document.getElementById("uplacenodosada").innerHTML);
	var total=strip_float_format(document.getElementById("Ukupno").innerHTML);
	document.getElementById("ukupnodugovanje").innerHTML=number_format(total-uplaceno,2);
	var zauplatu=total-uplaceno;
	var rata=strip_float_format(document.getElementById("ugovorena_rata").innerHTML);
	var br_rata=strip_float_format(document.getElementById("placanje_rate").innerHTML);
	var a = parseFloat(document.getElementById("placanje_avans").innerHTML.replace("%",""));
	if (zauplatu>rata && br_rata>0 && a<100)
		zauplatu=rata;
	document.getElementById("zauplatu2").innerHTML=number_format(zauplatu,2);
	document.getElementById("zauplatu1").innerHTML=number_format(zauplatu,2);
}

function CalculateUgovorenaVrednost() {
	CalculateAvans();
	document.getElementById("ugovorena_vrednost").innerHTML=document.getElementById("Ukupno").innerHTML;
}

function updateCenovnik(response){
	var tmp = response.split(/\{!\}/);
	var date = tmp[0];
	var cenovnik_id=tmp[1];
	response = tmp[2];
	//ypAjaxForm('<textarea>'+response+'<texarea>');
	if(cenovnik_id>0){
		var cen=document.getElementById("cenovnik");
		cen.parentNode.removeChild(cen);
		cen.innerHTML=response;
		document.getElementById("divCenovnik").appendChild(cen);
		var tbl = document.getElementById("stavkeTable");
		while (tbl.rows.length>2)
			tbl.deleteRow(1);
		document.getElementById("uplacenodosada").innerHTML=number_format(0,2);
		Predracun(tbl,null);
	}else{
		alert("Ne postoji cenovnik za zadati datum");
	}
}

function predracunDuplicateStavkaRow(row,e)
{
	e.stopPropagation();
	var ctrl = e && e.ctrlKey;
	var numRows = row.parentNode.getElementsByTagName("tr").length
	var tbl = row.parentNode;
	if(ctrl){
		if(numRows>3){
			if(confirm("Da li želite da obrišete stavku?")){
				row.cells[COLCena].innerHTML="0";
				Predracun(tbl,row);
				row.parentNode.removeChild(row);
			}
		}
	}else{
		if(confirm("Potvrdite dodavanje novog reda")){
			var cl = row.cloneNode(true);
			var tds = cl.getElementsByTagName("td");
			for(var i=0; i<tds.length; i++){
				tds[i].innerHTML = "&nbsp;";
				if(i>0){
					tds[i].onclick =OnCellStavkeTabeleClick;
				}
			}
			tbl.insertBefore(cl,row.nextSibling)
		}
	}
}

function OnCellStavkeTabeleClick(e) {
	var td=e.target;
	var row=td.parentNode;
	var tbl=row.parentNode;
	var combo = document.getElementById("cenovnik");
	//uzmi artikl
	var artikl;
	if (combo.selectedIndex>0)
		artikl=combo.options[combo.selectedIndex].innerHTML;
	else artikl="";
	var parent = combo.parentNode;
	if (td.cellIndex==COLNaziv) {
		parent.removeChild(combo);
		if (parent.tagName=="TD")
			parent.innerHTML=artikl;
		//postavi selected item u combo
		combo.selectedIndex=0;
		for (var i=0;i<combo.options.length;i++)
			if (combo.options[i].innerHTML==td.innerHTML)
			{
				combo.selectedIndex=i;
				break;
			}
		td.innerHTML="";
		td.appendChild(combo);
		combo.style.display="block";
		combo.onclick=function(e){
			e.stopPropagation();
		}
		combo.onchange=function(){
			var td=this.parentNode;
			var row=td.parentNode;
			row.cells[COLKolicina].innerHTML="1";
			row.cells[COLCena].innerHTML=number_format(this.options[this.selectedIndex].getAttribute("cena"),2,",",".");
			row.cells[COLPopustPerc].innerHTML=number_format(0,2,",",".");
			row.cells[COLPopust].innerHTML=number_format(0,2,",",".");
			Predracun(row.parentNode,row);
			e.stopPropagation();
		}
		combo.onblur=function(){
			var combo = document.getElementById("cenovnik");
			var parent = combo.parentNode;
			parent.removeChild(combo);
			if (parent.tagName=="TD")
			{
				var artikl;
				if (combo.selectedIndex>0)
					artikl=combo.options[combo.selectedIndex].innerHTML;
				else artikl="";
				parent.innerHTML=artikl;
			}
			document.getElementById("divCenovnik").appendChild(combo);
			combo.style.display="none";
		}
	}
	else {
		if (combo.style.display!="none")
		{
			if (parent.tagName=="TD")
			{
				parent.innerHTML=artikl;
				parent.appendChild(combo);
			}
			combo.style.display="none";
		}
		var nval = prompt("nova vrednost:",td.innerHTML.replace("&nbsp;"," "));
		if(nval===null){
			return;
		}else if(!nval.length){
			if(!confirm("Da li želite da izbrišete vrednost?")){
				return;
			}
		}
		switch (td.cellIndex)
		{
			case COLKolicina: //kolicina
			case COLPopust: //popust
			case COLPopustPerc:
				if (td.cellIndex==COLPopust)
					td.parentNode.cells[COLPopustPerc].innerHTML="0";
				else if(td.cellIndex==COLPopustPerc)
					td.parentNode.cells[COLPopust].innerHTML="0.00";
				if (td.cellIndex!=COLKolicina)
					td.innerHTML =number_format(strip_number_format(nval),2,",",".");
				else td.innerHTML =nval;
				break;
			default:
				return;
		}
		//izracunaj novi predracun
		Predracun(tbl,row);
	}
	e.stopPropagation();
}

function Predracun(tbl,row)
{
	PredracunArtikla(tbl,row);
	//4 polja su span-ovana
	for (var colIndex=SPANColsInLastRow;
		colIndex<tbl.rows[0].cells.length; colIndex++)
		if (colIndex!=COLPopustPerc)
			PredracunSumColumn(tbl.rows,colIndex);
	with (tbl.rows[tbl.rows.length-1]) {
		document.getElementById("OsnovicaPDV").innerHTML=
			number_format(strip_float_format(cells[COLKolCena-SPANColsInLastRow+1].innerHTML)
				-strip_float_format(cells[COLPopust-SPANColsInLastRow+1].innerHTML),
			2,",",".");
		document.getElementById("IznosPDV").innerHTML=cells[COLPDV-SPANColsInLastRow+1].innerHTML;
		document.getElementById("Ukupno").innerHTML=cells[COLUkupno-SPANColsInLastRow+1].innerHTML;
		document.getElementById("zauplatu1").innerHTML=cells[COLUkupno-SPANColsInLastRow+1].innerHTML;
	}
	CalculateUgovorenaVrednost();
}

function PredracunArtikla(tbl,row) {
	if (row==null)
		return;
	var kol = strip_float_format(row.cells[COLKolicina].innerHTML);
	var cenaJedinicna = strip_float_format(row.cells[COLCena].innerHTML);
	var cena = kol * cenaJedinicna;
	row.cells[COLKolCena].innerHTML=number_format(cena,2,",",".");
	var popust = strip_float_format(row.cells[COLPopust].innerHTML);
	var popustPerc = strip_float_format(row.cells[COLPopustPerc].innerHTML);
	if (popust>0)
		row.cells[COLPopustPerc].innerHTML=number_format(popust/cena*100,2);
	else if (popustPerc>0)
	{
		popust=popustPerc/100*cena;
		row.cells[COLPopust].innerHTML=number_format(popust,2,",",".");
	}
	cena-=popust;
	var porez = parseFloat(document.getElementById("stavkeTable").getAttribute("porez"));
	porez=cena*porez/100;
	row.cells[COLPDV].innerHTML=number_format(porez,2,",",".");
	cena+=porez;
	var tdLastInRow = row.cells[row.cells.length - 1]; // poslednje polje, tekuci red
	tdLastInRow.innerHTML = number_format(cena,2,",",".");
}

function PredracunSumColumn(rows,colIndex)
{
	var sum = 0;
	for (var rowIndex=1;rowIndex<rows.length-1;rowIndex++)
		sum += strip_float_format(rows[rowIndex].cells[colIndex].innerHTML);
	//prva cetiri polja poslednjeg reda su span-ovana
	rows[rows.length - 1].cells[colIndex-SPANColsInLastRow+1].innerHTML = number_format(sum,2,",",".");
}
